<template>
  <div>
    <el-dialog
      :title="title"
      :closeOnClickModal="false"
      :visible.sync="show"
      fullscreen
      lock-scroll
      width="750px">
      <el-form 
        label-position="right" 
        :model="selectTable"
        ref="addForm" 
        label-width="120px" 
        :inline="false"
        autocomplete="off"
        >
        <el-row>
          <el-col :span="12">
            <el-form-item label="公司名称" prop="companyName" :rules="[rule[0]]">
              <el-input placeholder="请输入" v-model="selectTable.companyName" maxLength="100"></el-input>
            </el-form-item>
          </el-col>
          
          <el-col :span="12">
            <el-form-item label="状态" prop="status" :rules ="[rule[0]]">
              <el-switch
                v-model="selectTable.status"
                :active-value="0"
                :inactive-value="1"
                active-text="启用"
                inactive-text="停用"
                >
              </el-switch>
            </el-form-item>
          </el-col>
        </el-row>
        
        <el-row>
          <el-col :span="12">
            <el-form-item label="公司logo" prop="companyLogo" :rules ="[rule[0]]">
              <singleImage v-model="selectTable.companyLogo"></singleImage>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="公司标示" prop="biref" :rules ="[rule[0]]">
              <singleImage v-model="selectTable.biref"></singleImage>
            </el-form-item>
          </el-col>
        </el-row>
        
        <el-form-item label="上线日期" prop="onlineTime" :rules="[rule[0]]">
          <el-date-picker
            value-format="timestamp"
            v-model="selectTable.onlineTime"
            type="date"
            placeholder="选择日期">
          </el-date-picker>
        </el-form-item>
            
        <el-form-item label="注册地址" prop="registeredAddress" :rules="[rule[0]]">
          <el-input placeholder="请输入" v-model="selectTable.registeredAddress" maxLength="100"></el-input>
        </el-form-item>
            
        <el-form-item label="注册资本" prop="registeredCapital" :rules="[rule[0]]">
          <el-input placeholder="请输入" v-model="selectTable.registeredCapital" maxLength="100"></el-input>
        </el-form-item>
            
        <el-form-item label="公司官网" prop="companyWebsite">
          <el-input placeholder="请输入" v-model="selectTable.companyWebsite" maxLength="100"></el-input>
        </el-form-item>
            
        <el-form-item label="公司软件(APP)" prop="companySoftWare">
          <el-input placeholder="请输入" v-model="selectTable.companySoftWare" maxLength="100"></el-input>
        </el-form-item>
            
        <el-form-item label="收益范围" prop="incomeRange" :rules="[rule[0]]">
          <el-input placeholder="请输入" v-model="selectTable.incomeRange" maxLength="100"></el-input>
        </el-form-item>
            
        <el-form-item label="公司电话" prop="companyPhone" :rules="[rule[0]]">
          <el-input placeholder="请输入" v-model="selectTable.companyPhone" maxLength="100"></el-input>
        </el-form-item>
            
        <el-form-item label="企业执照代码" prop="companyCode">
          <el-input placeholder="请输入" v-model="selectTable.companyCode" maxLength="100"></el-input>
        </el-form-item>
            
        <el-form-item label="公司法人" prop="companyPerson" :rules="[rule[0]]">
          <el-input placeholder="请输入" v-model="selectTable.companyPerson" maxLength="100"></el-input>
        </el-form-item>
            
        <el-form-item label="公司联系人" prop="companyContactsPerson" :rules="[rule[0]]">
          <el-input placeholder="请输入" v-model="selectTable.companyContactsPerson" maxLength="100"></el-input>
        </el-form-item>
            
        <el-form-item label="Icp备案号" prop="icpRecord" :rules="[rule[0]]">
          <el-input placeholder="请输入" v-model="selectTable.icpRecord" maxLength="20"></el-input>
        </el-form-item>
        
        <el-row>
          <el-col :span="12">
            <el-form-item label="券商协议" prop="brokersAgreement">
              <singleImage v-model="selectTable.brokersAgreement"></singleImage>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="行权单" prop="rightAction">
              <singleImage v-model="selectTable.rightAction"></singleImage>
            </el-form-item>
          </el-col>
        </el-row>
        
        <el-form-item label="排序序号" prop="sortNumber" :rules="[rule[0]]">
          <el-input-number v-model="selectTable.sortNumber" controls-position="right" style="width: 120px;" :min="0"></el-input-number>
        </el-form-item>

        <el-form-item label="公司简介" prop="companyProfile">
          <el-input type="textarea" placeholder="请输入" v-model="selectTable.companyProfile" maxLength="200"></el-input>
        </el-form-item>
        
        <el-form-item label="详情" prop="companyDetails" :rules ="[rule[0]]">
          <editor v-model="selectTable.companyDetails"
            :config="{initialFrameHeight:200}">
          </editor>
        </el-form-item>
        
      </el-form>
      <div slot="footer" class="dialog-footer" v-loading="pload">
        <el-button @click="show = false">取 消</el-button>
        <el-button type="primary" @click="submitForm('addForm')">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {companySave,companyModify,companyGetById} from '@/utils/api'
import singleImage from '@/components/Upload/singleImage'
export default {
  components: {
  	editor : () => import('@/components/Ueditor'),
  	singleImage
  },
  props: {
    id: Number
  },
  created () {
    if (this.id) {
      this.getData()
    } else {
      this.pload = false
    }
  },
  watch: {
    show (newVal) {
      this.$parent.edit = false;
    }
  },
  computed: {
    title () {
      if (this.id) {
        return '修改';
      }
      else {
        return '添加';
      }
    }
  },
  data () {
    return {
      pload: false,
      rule: [
        { required: true, message: '必填项', trigger: 'change' }
      ],
      show: true,
      selectTable: {}
    }
  },
  methods: {
    async submitForm(formName) {
      await this.$refs[formName].validate();
      try {
        this.pload = true
        if (this.id) {
          await companyModify(this.selectTable)
        }
        else {
          await companySave(this.selectTable)
        }
        this.$notify({title: '操作成功', type: 'success', duration: 5000})
        this.$parent.edit = false;
        this.$emit('getData');
      } catch(e) {
        this.pload = false
      }
    },
    async getData(){
      this.pload = true
      const Datas = await companyGetById({
        companyId: this.id
      })
      this.selectTable = Datas
      this.pload = false
    }
  }
}
</script>